/**
 * 
 */
package com.alkandros.linesmash.ui.signup;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.interfaces.IConstants;
import com.alkandros.linesmash.interfaces.IURLConstants;
import com.alkandros.linesmash.location.ILocationCallBack;
import com.alkandros.linesmash.manager.APIManager;
import com.alkandros.linesmash.manager.AlertManager;
import com.alkandros.linesmash.manager.CustomProgressDialog;
import com.alkandros.linesmash.manager.Utils;
import com.alkandros.linesmash.ui.BaseActivity;


/**
 * @author Jiju Induchoodan
 * 
 */
public class SignUp extends BaseActivity implements OnClickListener,ILocationCallBack,
		IURLConstants,IConstants {

	private Intent intent;
	private TextView submit;
	private EditText edtFirstname;
	private EditText edtLastName;
	private EditText edtEmail;
	private EditText edtPassword;
	private EditText edtRePassword;
	private CustomProgressDialog progress;
	private Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);
		context =this;
		initialiseUI();
	}

	private void initialiseUI() {
		setHeading(R.string.sign_up);
		setLeftButton();
		setRightButton();

		edtFirstname 	= (EditText) findViewById(R.id.edt_firstname);
		edtLastName 	= (EditText) findViewById(R.id.edt_lastname);
		edtEmail 		= (EditText) findViewById(R.id.edt_email);
		edtPassword 	= (EditText) findViewById(R.id.edt_password);
		edtRePassword 	= (EditText) findViewById(R.id.edt_repassword);
		submit 			= (TextView) findViewById(R.id.submit);
		submit.setOnClickListener(this);
		progress  = CustomProgressDialog.show(context, false);
	}

	@Override
	public void onClick(View v) {
		if (v == submit) {

			if (!Utils.checkInternetConnection(SignUp.this)) {
				AlertManager.shorttoastMessage(SignUp.this,
						getString(R.string.Internet_check));
			}

			else if ((edtFirstname.getText().toString().trim().equals(""))
					|| (edtLastName.getText().toString().trim().equals(""))
					|| (edtEmail.getText().toString().trim().equals(""))
					|| (edtPassword.getText().toString().trim().equals(""))
					|| (edtRePassword.getText().toString().trim().equals(""))) {
				AlertManager.shorttoastMessage(SignUp.this,
						getString(R.string.Please_fill_username_password));
			}

			else if (!Utils.isValidEmail(edtEmail.getText().toString())) {

				AlertManager.shorttoastMessage(SignUp.this,
						getString(R.string.Please_fill_vaild_email));
			}

			else if (!edtPassword.getText().toString().trim().equals(edtRePassword.getText().toString().trim())) {

				AlertManager.shorttoastMessage(SignUp.this,
						"Password Mismatch");
			}

			else {
				SignUpTask task = new SignUpTask();
				task.execute();
			}
			
			//SignUpTask task = new SignUpTask();
		//	task.execute();

			// intent = new Intent(this,PaymentSelection.class);
			// startActivity(intent);
		}
	}

	class SignUpTask extends AsyncTask<Void, Void, Void> {
		private APIManager manager;
		private boolean isException = false;
		private String response;
		private String[] names = {"email", "password", "name","lastname", "phonenumber" };
		private String[] values = { edtEmail.getText().toString(),
				edtPassword.getText().toString(),
				edtFirstname.getText().toString(),
				edtLastName.getText().toString(),
				edtLastName.getText().toString()
				};

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progress.dismiss();
			if(isException)
			{
				AlertManager.shorttoastMessage(SignUp.this,getString(R.string.common_error_message));
			}
			else
			{
				if (response.contains(ERROR_CODE)) {
					AlertManager.shorttoastMessage(SignUp.this,getString(R.string.invalid_credentails));
				} else {
					intent = new Intent(SignUp.this, PaymentSelection.class);
					startActivity(intent);
					finish();
				}
			}
			
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			manager = new APIManager(URL_SIGNUP);
			progress.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				response = manager.commonPostData(URL_SIGNUP, names, values);
			} catch (Exception e) {
				e.printStackTrace();
				isException = true;
			}
			return null;
		}

	}

	/* (non-Javadoc)
	 * @see com.alkandros.linesmash.location.ILocationCallBack#setResult(java.lang.String)
	 */
	@Override
	public void setResult(String address) {
		// TODO Auto-generated method stub
		
	}

}
